
import requests
import json
import pymongo
mongo =pymongo.MongoClient(host = "myserver")
answers = mongo.get_database("zhihu").get_collection("answer")
def url(q,off, limit=20):
    url =  r'''https://www.zhihu.com/api/v4/questions/{q}/answers?include=data%5B%2A%5D.is_normal%2Cadmin_closed_comment%2Creward_info%2Cis_collapsed%2Cannotation_action%2Cannotation_detail%2Ccollapse_reason%2Cis_sticky%2Ccollapsed_by%2Csuggest_edit%2Ccomment_count%2Ccan_comment%2Ccontent%2Ceditable_content%2Cvoteup_count%2Creshipment_settings%2Ccomment_permission%2Ccreated_time%2Cupdated_time%2Creview_info%2Crelevant_info%2Cquestion%2Cexcerpt%2Crelationship.is_authorized%2Cis_author%2Cvoting%2Cis_thanked%2Cis_nothelp%2Cis_labeled%2Cis_recognized%2Cpaid_info%2Cpaid_info_content%3Bdata%5B%2A%5D.mark_infos%5B%2A%5D.url%3Bdata%5B%2A%5D.author.follower_count%2Cbadge%5B%2A%5D.topics&20=5&0=100&platform=desktop&sort_by=default&offset={off}&limit={limit}'''.format(q=q,off=off, limit = limit)
    return url
def doGet(_url):
    h = {
        "User-Agent":r"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36",
        "Cookie":r'''_zap=d7911c4f-03ba-4a00-ae04-cef542ca645e; d_c0="AMDtAmsztw-PTmn0-h-FjEv9fqGGSXppYyQ=|1562772438"; z_c0="2|1:0|10:1562772444|4:z_c0|92:Mi4xNS11LUFRQUFBQUFBd08wQ2F6TzNEeVlBQUFCZ0FsVk4zRkVUWGdEbHlBbEVjYThNRHhaUGd4YXhwejBGRG9ub3N3|8a2ce5b85b7ed4ee304d086e132e6455cf01b0afa118a6cbfe2826237abfb6f5"; __utmv=51854390.100-1|2=registration_date=20150531=1^3=entry_date=20150531=1; _xsrf=867b0f74-ca7d-4a4a-8092-c7474a90400d; __utmc=51854390; __utma=51854390.818807665.1564410565.1564669537.1564853993.3; __utmz=51854390.1564853993.3.3.utmcsr=zhihu.com|utmccn=(referral)|utmcmd=referral|utmcct=/people/dayinday/collections; tst=r; q_c1=e850842d985d4f9e8a3e5de2389c469b|1565406865000|1562772737000; tgw_l7_route=116a747939468d99065d12a386ab1c5f'''
    }
    return requests.get(_url,headers = h).text
def scarw(question):
    off = 0
    existing = loadExstsingAnswers(question)
    total = -1
    while total == -1 or off < total:
        s = doGet(url(question,off))
        data = json.loads(s)
        if total == -1:
            total =data["paging"]["totals"]
        r = []
        for x in data["data"]:
            x["_id"] = x["id"] = int(x["id"])
            x["q_id"] = int(x["question"]["id"])
            if x["q_id"] not in existing:
                r.append(x)
        answers.insert_many(r)
        off +=20
def loadExstsingAnswers(id):
    r = answers.find(filter={"q_id":id},projection= ["_id"])
    return set( x["q_id"] for x in r)




